//
//  FKCourseCell.m
//  NeteaseEdu
//
//  Created by xmc06 on 16/9/27.
//  Copyright © 2016年 zmosa. All rights reserved.
//

#import "FKCourseCell.h"
#import "UIView+FK.h"
#import "FKCourse.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>

static NSString *const courseCell = @"courseCell";

@interface FKCourseCell ()
@property (nonatomic, weak) UIImageView *pictureView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIImageView *timeView;
@property (nonatomic, weak) UILabel *learnedLabel;
@end

@implementation FKCourseCell

+ (instancetype)courseCellWithTableView:(UITableView *)tableView
{
    FKCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:courseCell];
    if (!cell) {
        cell = [[FKCourseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:courseCell];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIImageView *pictureView = [[UIImageView alloc] init];
    [self addSubview:pictureView];
    self.pictureView = pictureView;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    titleLabel.font = [UIFont systemFontOfSize:14];
    [titleLabel sizeToFit];
    titleLabel.numberOfLines = 0;
    
    UIImageView *timeView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"endTime_11x11_"]];
    [self addSubview:timeView];
    self.timeView = timeView;
    
    UILabel *learnedLabel = [[UILabel alloc] init];
    [self addSubview:learnedLabel];
    self.learnedLabel = learnedLabel;
    learnedLabel.font = [UIFont systemFontOfSize:12];
    [learnedLabel sizeToFit];
    learnedLabel.textColor = fk_globalGreenColor;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.pictureView.frame = CGRectMake(10, 10, 128, 70);
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.pictureView.mas_right).offset(10);
        make.right.equalTo(self.mas_right).offset(-20);
        make.top.equalTo(self.pictureView.mas_top);
    }];
    
    CGFloat timeViewX = CGRectGetMaxX(self.pictureView.frame) + 10;
    CGSize timeViewSize = self.timeView.image.size;
    CGFloat timeViewY = CGRectGetMaxY(self.pictureView.frame) - timeViewSize.height;
    self.timeView.frame = (CGRect){{timeViewX, timeViewY}, timeViewSize};
    
    [self.learnedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeView.mas_right).offset(5);
        make.centerY.equalTo(self.timeView.mas_centerY);
    }];
}

- (void)setCourse:(FKCourse *)course
{
    _course = course;
    
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:course.bigPicUrl] placeholderImage:nil];
    self.titleLabel.text = course.resName;
    self.learnedLabel.text = [NSString stringWithFormat:@"已学习%@/%@课", course.learnedResCount, course.totalResCount];
}

@end
